// pages/member/member.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    orgId:'',
    count:0,
    number:'',
    isAll:false,
    define:false,
    parts:[],
    isModify1:false,
    activityId:'',
    isSou:false,
    souname:'',
    member:[],
  },
  focusbtn(){
    this.setData({
      isSou:true
    })
  },
  //搜索
  souinput(e){
    var souname = e.detail.value.replace(/(^\s*)(\s*$)/g, "")
    this.setData({
      souname: souname
    })
    // console.log('搜索', this.data.souname)
    if (this.data.isModify1 == 'false') {
      this.getlist()
    } else if (this.data.isModify1 == 'true') {
      this.getlists()
    }
  },
  //获取组织列表
  getlist(){
    var that = this
    // console.log(wx.getStorageSync('uid'))
    app.req({
      url: "orgmini/org/mySimplifyOrgList?uid=" + wx.getStorageSync('uid'),
      data: {
        uid: wx.getStorageSync('uid'),
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          // console.log(res.data)
          var count = 0;
          for (var i = 0; i < res.data.length; i++){
            for (var j = 0; j < that.data.member.length; j++) {
              res.data[i].flag = false;
              if (that.data.member[j] == res.data[i].id){
                res.data[i].flag = true;
                count++;
                break;
              }
            }
          }
          that.setData({
            list: res.data,
            number:res.data.length,
            // define: true,
            count: count
          })
          if (count == res.data.length){
            that.setData({
              define: true,
            })
          }
        }
      }
    });
  },
  getlists(){
    var that = this
    app.req({
      url: "wechat/activity/getStructureDetail",
      data: {
        orgId: that.data.orgId,
        id: that.data.activityId,
        name: that.data.souname
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          var num =0
          for(var i= 0;i<res.data.length;i++){
            if(res.data[i].isChoose==1){
              num++
            }
          }
          if(num==res.data.length){
            that.setData({
              define:false
            })
          }
          that.setData({
            list: res.data,
            number: res.data.length,
            count:num
          })
        }
      }
    });
  },
  singlebtn(e){
    var that = this
    var index = e.currentTarget.dataset.index
    var arr = that.data.list
    var count = that.data.count
    var cz=0
    var num =0
      if(arr[index].flag){
        arr[index].flag = false
        count--
      }else{
        arr[index].flag = true
        count++
      }

    for (var i = arr.length - 1; i >= 0; i--) {
      if (arr[i].flag) {
        num++
      } 
    }
    if(num==arr.length){
      that.setData({
        define:true,
      })
    }else{
      that.setData({
        define: false,
      })
    }

      that.setData({
        list:arr,
        count:count,
      })
    console.log('单选列表', that.data.count)
  },
  //全选
  allbtn(){
    var that = this
    var list = that.data.list
    var arry = []
    var count = that.data.count
    if(that.data.define){
      for (var i = 0; i < list.length; i++) {
        if (list[i].isChoose !=1){
          if (list[i].flag){
            list[i].flag = false
            count--
          }
        }
      }
      that.setData({
        define: false
      })
    }else{
      for (var i = 0; i < list.length; i++) {
        if (list[i].isChoose != 1) {
          if (!list[i].flag){
            list[i].flag = true
            arry.push(list[i].id)
            count++
          }
        }
      }
      that.setData({
        define: true
      })
    }
    // if(count==list.length){
    //   that.setData({
    //     define: false
    //   })
    // }else{
    //   that.setData({
    //     define: true
    //   })
    // }
    that.setData({
      list: list,
      count: count,
    })
    console.log('全选列表',that.data.count)
  },
  //确定
  okbtn(){
    var that =this
    for (var i = 0; i < that.data.list.length; i++) {
      if (that.data.list[i].flag){
        that.data.parts.push({
          id: that.data.list[i].id,
          orgUrl: that.data.list[i].orgUrl,
          orgName: that.data.list[i].orgName
        })
      }
    }
    // console.log('选中列表', that.data.parts)
    let pages = getCurrentPages(),
    prevPage = pages[pages.length - 2];
    if (that.data.parts.length != 0){
      prevPage.setData({
        'addOrg': that.data.parts,
        showAddOrg: true,
        organizationChecked:true,
      })
    }else{
      prevPage.setData({
        'addOrg': that.data.parts,
        showAddOrg: false,
        organizationChecked: false,
      })
    }
    wx.navigateBack({
      delta: 1
    })
  },
  //删除元素
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
    //   orgId: options.id,
    //   // isModify1: options.isModify1,
    //   activityId:options.actid
      member: JSON.parse(options.addOrg)  
    })
    // console.log('活动id', this.data.activityId,this.data.isModify1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getlist()

    // console.log('修改', this.data.isModify1)
    // var that = this
    // if (that.data.isModify1=='false'){
    //   console.log('修改1', that.data.isModify1)
    // } else if (that.data.isModify1=='true'){
    //   // that.getlists()
    //   console.log('修改2', that.data.isModify1)
    // }
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})